package com.xpgk.mvc.statistic.customerInfo;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.sql.visitor.functions.If;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.expr.ast.Id;
import com.sun.java_cup.internal.runtime.virtual_parse_stack;
import com.sun.org.apache.bcel.internal.classfile.Code;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerHabit;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.binShopService.BinShopServiceService;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.binShopService.CustomerServiceMonthNurse;
import com.xpgk.mvc.binShopService.CustomerServiceNurse;
import com.xpgk.mvc.binShopService.ServiceShaping;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.collection.binShopService.BinShopColServiceService;
import com.xpgk.mvc.productOrgExperience.ProductOrgExperience;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.productStock.ProductStock;
import com.xpgk.mvc.productStock.ProductStockInfo;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticSQL;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.tools.ToolDateTime;
import com.xpgk.tools.ToolRandoms;

@Service(name = CustomerInfoStatisticService.serviceName)
public class CustomerInfoStatisticService extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(CustomerInfoStatisticService.class);
	public static final String serviceName = "customerInfoStatisticService";
	private BinShopServiceService binShopServiceService;
	
	
	public   List<Record>  LookCustomerMonthEverydayKeyIndex(String organIds,String customerIds,String startTime,String endTime ) throws ParseException
	{	
		/*根据起始月和结束月获取之间所有月份
		 * 
		 */
		ArrayList<String> monthresult = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();
		min.setTime(sdf.parse(startTime));
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
		
		max.setTime(sdf.parse(endTime));
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
		
		Calendar curr = min;
		while (curr.before(max)) {
			monthresult.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}
		
		Customer  cus=Customer.dao.findById(customerIds);
		
		 List<Record>  monthEverydayKeyIndex=new  ArrayList<>();
		for(int i=0;i<monthresult.size();i++)
		{	
			Record  record=new Record();
			String likemonth="%"+monthresult.get(i)+"%";
			//表格-时间
			record.set("servicedate", monthresult.get(i));
			
			Calendar  cal=Calendar.getInstance();
			cal.setTime(sdf.parse(monthresult.get(i)));
			//根据月份获取服务记录
			String serviceSql=getSqlMy(CustomerService.sqlId_selByMonth);
			List<CustomerService>  dycs=CustomerService.dao.find(serviceSql,customerIds,organIds,likemonth);
			if(dycs!=null && dycs.size()>0)
			{	
				for(CustomerService  cs:dycs)
				{	
					//获取日期为当月第几天
					Date  d=cs.getTimestamp(CustomerService.column_createDate);
					if(d!=null){
						cal.setTime(d);
					}
					
					int day=cal.get(Calendar.DAY_OF_MONTH);
					//表格-日体重
					record.set(day+"", String.format("%.2f", cs.getFloat(CustomerService.column_weight)));
				}
				 DecimalFormat df = new DecimalFormat(".0");
			   
				//表格-当月最后一天体重
				record.set("lastdayweight", dycs.get(dycs.size()-1).getFloat(CustomerService.column_weight));
				//标准体重
				float bzweight=cus.getCustomerAgreement().getFloat(CustomerAgreement.column_std_weight);
				//表格-未减斤数（当月最后一条体重减去标准体重）
				record.set("nodelweight",  Float.parseFloat(df.format(dycs.get(dycs.size()-1).getFloat(CustomerService.column_weight)-bzweight)));
				//初始体重
				float csweight=cus.getCustomerDetail().getFloat(CustomerDetail.column_firstWeight);
				float ljdelweight=csweight-dycs.get(dycs.size()-1).getFloat(CustomerService.column_weight);
				
				//表格-累计减重斤数（初始体重减去当月最后一条体重）
				record.set("totaldelweight",  String.format("%.1f", ljdelweight));
				//应减斤数
				float yjweight=cus.getCustomerDetail().getFloat(CustomerDetail.column_delWeight);
				
				//表格-累计减重率（累计减重斤数÷应减斤数）
				record.set("totaldelratio", String.format("%.2f", ljdelweight/yjweight*100)+"%");
				//表格-当月实减斤数(当月第一条的上次体重减去当月最后一条的体重)
				record.set("monthdelweight",  String.format("%.1f", dycs.get(0).getFloat(CustomerService.column_lastWeight)-dycs.get(dycs.size()-1).getFloat(CustomerService.column_weight)));
				
			}
			//表格-到店次数
			record.set("tostorenum", dycs.size());
			
			if(dycs.size()>21)
			{
				//表格-到店次数比率21次合格率
				record.set("tostoreratio", "合格");
			}else
			{
				//表格-到店次数比率21次合格率
				record.set("tostoreratio", "不合格");
			}
			
			
			
			
			
			/*获取当月减重目标
			 * 
			 */
			Date m = sdf.parse(monthresult.get(i));
			//获取某月第一天和最后一天
			Map<String, Date>  dateMap=ToolDateTime.getMonthDate(m);
			Date  startDate=dateMap.get("start");
			Date  endDate=dateMap.get("end");
			SimpleDateFormat sdf1 =   new SimpleDateFormat( "yyyy-MM-dd" );
			String cs1=sdf1.format(startDate); 
			String cs2=sdf1.format(endDate);
			String  delweightsql=getSqlMy(DailyNursingStatisticSQL.sqlId_selDeltaget);
			//获取减重目标
			List<DelWeiTarget>  dtList=DelWeiTarget.dao.find(delweightsql,customerIds,cs2,cs2,cs1,cs1);
			if(dtList!=null && dtList.size()>0)
			{
				//获取第一条的平均减重
				float  delaverage=dtList.get(0).getFloat(DelWeiTarget.column_delWeiTarget)/(ToolDateTime.getDateDaySpace(dtList.get(0).getDate(DelWeiTarget.column_startTime), dtList.get(0).getDate(DelWeiTarget.column_endTime))+1);
				//获取当月第一天到第一期减重应减的斤数
				float  startdel=(ToolDateTime.getDateDaySpace(startDate,  dtList.get(0).getDate(DelWeiTarget.column_endTime))+1)*Float.parseFloat(String.format("%.2f", delaverage));
				//获取最后一条平均减重
				float  lastaverage=dtList.get(dtList.size()-1).getFloat(DelWeiTarget.column_delWeiTarget)/(ToolDateTime.getDateDaySpace(dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_startTime), dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_endTime))+1);
				//获取当月最后一天到最后一期减重应减的斤数
				float  enddel=ToolDateTime.getDateDaySpace(dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_startTime),endDate)*Float.parseFloat(String.format("%.2f", lastaverage));
				//总减重目标
				float  totaldel=startdel+enddel;
				for(int n=1;n<dtList.size()-1;n++)
				{
					totaldel+=dtList.get(n).getFloat(DelWeiTarget.column_delWeiTarget);
				}
				//表格-当月目标
				record.set("monthtarget", totaldel);
			}
			
			if(record.getFloat("monthtarget")!=null && record.getFloat("monthdelweight")!=null && record.getFloat("monthtarget")!=0f && record.getFloat("monthdelweight")!=0f)
			{	//表格-当月目标达成率(当月实减斤数÷当月目标)
				record.set("monthtargetratio", String.format("%.2f", record.getFloat("monthdelweight")/record.getFloat("monthtarget")*100)+"%");
			}
			if(record.getFloat("nodelweight")!=null && record.getFloat("nodelweight")<=0)
			{
				//表格-是否达标
				record.set("isdb", "是");
			}else
			{
				//表格-是否达标
				record.set("isdb", "否");
			}
			monthEverydayKeyIndex.add(record);
		}
		
		return  monthEverydayKeyIndex;
	}
	
	
	public boolean deleteService(String serviceIds,String userIds,String organIds) throws ParseException{
		
		boolean flag = false;
		float ybg = 0f;
		float bbg = 0f;
		float kbg = 0f;
		float gbg = 0f;
		float dtg = 0f;
		float xtg = 0f;
		float zfrg = 0f;
		float yfrg = 0f;
		float ybjy = 0f;
		float bbjy = 0f;
		float kbjy = 0f;
		float gbjy = 0f;
		float dtjy = 0f;
		float xtjy = 0f;
		float zfrjy = 0f;
		float yfrjy = 0f;
		float sumjy = 0f;
		float sumg = 0f;
		Map<String, Object> m = new HashMap<String, Object>();
		
		String sqlProductjy = "SELECT * FROM sb_customer_product where customerID = ? and dataStatus in ('1','2') and organids = ? and productID = '95e2ae17462e4d0eb1dc84bc63bc478a'";
		String sqlProductg = "SELECT * FROM sb_customer_product where customerID = ? and dataStatus in ('1','2') and organids = ? and productID = 'daaba791f4104ae98504a1bc421e0d8d'";
		String sqlshaping = "SELECT * from sb_customer_service_shaping where serviceIds = ? and dataStatus in (1,2)";
		CustomerService customerService = CustomerService.dao.findById(serviceIds);
		String customerIds = customerService.getStr(CustomerService.column_customerID);
		Customer customer = Customer.dao.findById(customerService.getStr(CustomerService.column_customerID));
		String sqldetail = "select * from sb_customer_detail where customerIds = ? and organIds = ? and dataStatus in ('1','2')";
		CustomerDetail cdet = CustomerDetail.dao.findFirst(sqldetail,customerIds,organIds);
		boolean isXgk = ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_y).equals(ToolDateTime.format(customerService.getDate(CustomerService.column_startTime), ToolDateTime.pattern_y));
		boolean isdndyXgk = ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ym).equals(ToolDateTime.format(customerService.getDate(CustomerService.column_startTime), ToolDateTime.pattern_ym));
		String servicesql = getSqlMy(CustomerService.sqlId_selNextWeight);
		CustomerService cs0 = CustomerService.dao.findFirst(servicesql ,customerService.getDate(CustomerService.column_startTime),customerIds);
		if(cs0!=null){
			m.put("msg","只能删除最新一条服务记录");
		}else{
			
		
		if(customerService!=null){
			m.put("msg","200");
			//保存到店人数，服务人数，服务次数
			DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(organIds,customerService.getDate(CustomerService.column_startTime));
			if(dnt!=null){
				dnt.set(DailyNursingTotal.column_sddrs, dnt.getInt(DailyNursingTotal.column_sddrs)-1)
				.set(DailyNursingTotal.column_fwcs, dnt.getInt(DailyNursingTotal.column_fwcs)-1);
			if(isXgk){
				dnt.set(DailyNursingTotal.column_xgkddrs, dnt.getInt(DailyNursingTotal.column_xgkddrs)-1);
			}
			if(cdet.getStr(CustomerDetail.column_customerType)!=null && !cdet.getStr(CustomerDetail.column_customerType).equals(1)){
				
			
			if(customerService.getStr(CustomerService.column_isGg)!=null && customerService.getStr(CustomerService.column_isGg).equals("0"))
			{	
				float  cjs = customerService.getFloat(CustomerService.column_weight) - customerService.getFloat(CustomerService.column_lastWeight);
				float dcjs = Float.parseFloat(new DecimalFormat("#.0").format(cjs));
				
				if( dcjs < 0.00)
				{
					dnt.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs)-1)
						.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)-(-dcjs));
					if(isXgk){
						dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)
						.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)-(-dcjs));
					}
				}
				if(dcjs == 0.00)
				{
					dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs)-1);
				}
				if( dcjs > 0.00)
				{
					dnt.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)-1)
						.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)-dcjs);
				}
			}else if(customerService.getStr(CustomerService.column_isGg)!=null && customerService.getStr(CustomerService.column_isGg).equals("1")){
					String sql = "SELECT * from sb_customer_service  where DATE_FORMAT(startTime, '%Y-%m-%d') <  ?    AND    customerID = ?   and dataStatus in ('1','2')   ORDER BY  startTime   DESC   LIMIT 0,1";
					CustomerService last = CustomerService.dao.findFirst(sql,ToolDateTime.format(customerService.getDate(CustomerService.column_startTime), ToolDateTime.pattern_ymd),customerIds);
					
						if(last!=null && last.getStr(CustomerService.column_isGg)!=null &&last.getStr(CustomerService.column_isGg).equals("1")){
							dnt.set(DailyNursingTotal.column_ggrs, dnt.getInt(DailyNursingTotal.column_ggrs)-1);
						}else{
							float  cjs = customerService.getFloat(CustomerService.column_weight) - customerService.getFloat(CustomerService.column_lastWeight);
							float dcjs = Float.parseFloat(new DecimalFormat("#.0").format(cjs));
							
							if( dcjs < 0.00)
							{
								dnt.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs)-1)
									.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)-(-dcjs));
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)
									.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)-(-dcjs));
								}
							}
							if(dcjs == 0.00)
							{
								dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs)-1);
							}
							if( dcjs > 0.00)
							{
								dnt.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)-1)
									.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)-dcjs);
							}
							cdet.set(CustomerDetail.column_customerType, "0");
							cdet.update();
							dnt.set(DailyNursingTotal.column_dbrs, dnt.getInt(DailyNursingTotal.column_dbrs)-1);
							
						}
					
					
				
				
			}
			}
			
			if (!dnt.update()) {
				log.debug("更新日综合查询表失败");
				throw new RuntimeException("更新日综合查询表失败!");
			}
			
			
			
			
			}
			ServiceShaping serviceShaping = ServiceShaping.dao.findFirst(sqlshaping,serviceIds);
			if(customerService.getInt(CustomerService.column_dataStatus)==1){//如果服务不是补录的把产品、药贴入库
				//查询该门店是否有附加部位
				String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
				String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
				String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
				ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,organIds);
				ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,organIds);
				ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,organIds);
				/**
				 * 药贴数量如果不是0，将药贴入库
				 */
				int plasterCount = 0;
				if(customerService.getInt(CustomerService.column_plasterCount)!=null){
					 plasterCount = customerService.getInt(CustomerService.column_plasterCount);
				}
				
				if(plasterCount!=0){
					
					String sql=getSqlMy(Product.sqlId_selProByIds);
					Product p=Product.dao.findFirst(sql,"c3df895ee3b24d1193cb8156edc665b7",organIds);
					ProductOutin po = new ProductOutin();
					if(p!=null)
					{
						po.set(ProductOutin.column_organIds, organIds)
						.set(ProductOutin.column_date, new Date())
						.set(ProductOutin.column_style, "0")
						.set(ProductOutin.column_productIds,"c3df895ee3b24d1193cb8156edc665b7")
						.set(ProductOutin.column_number, plasterCount)
						.set(ProductOutin.column_typeIds,com.xpgk.mvc.codeSet.Code.proYt_ids )
						.set(ProductOutin.column_bz, "删除"+customer.getStr(Customer.column_name)+"的服务记录")
						.set(ProductOutin.column_userIds, userIds)
						.set(ProductOutin.column_gxsj, new Date())
						.set(ProductOutin.column_price, p.getFloat(ProductPrice.column_putPrice));
						if(!po.save())
						{
							log.debug("保存产品出入库失败");
							throw new RuntimeException("保存产品出入库失败!");
						}
					}else
					{
						log.debug("产品未设置价格");
						throw new RuntimeException("产品未设置价格!");
					}
					
					//更新产品库存表
					String  sptSql=getSqlMy(ProductStock.sqlId_selPSByOidsAndPids);
					ProductStock  ps=ProductStock.dao.findFirst(sptSql,organIds,"c3df895ee3b24d1193cb8156edc665b7");
					if(ps!=null)
					{
						//更新
						float  num=ps.getFloat(ProductStock.column_num)+1;
						ps.set(ProductStock.column_num, num)
							.set(ProductStock.column_gxsj, new Date());
						if(!ps.update())
						{
							log.debug("更新产品总库存失败");
							throw new RuntimeException("更新产品总库存失败!");
						}
						
					}else
					{
						ProductStock  psk=new ProductStock();
						psk.set(ProductStock.column_ids,ToolRandoms.getUuid(true))
							.set(ProductStock.column_organids, organIds)
							.set(ProductStock.column_productID, "c3df895ee3b24d1193cb8156edc665b7")
							.set(ProductStock.column_num, 1)
							.set(ProductStock.column_gxsj,new Date());
						if(!psk.save())
						{
							log.debug("保存产品总库存失败");
							throw new RuntimeException("保存产品总库存失败!");
						}
						ps=psk;
					}
					//更新库存明细
					SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
			    	String gxsj="%"+dateFormater.format(new Date())+"%";
					String  psiSql=getSqlMy(ProductStockInfo.sqlId_selByOidsAndPidsAndTime);
					ProductStockInfo  psi=ProductStockInfo.dao.findFirst(psiSql,organIds,po.getStr(ProductOutin.column_productIds),gxsj);
					if(psi!=null)
			    	{	
			    		//库存详细信息减少库存
			    		float num=psi.getFloat(ProductStockInfo.column_stock);
			    		psi.set(ProductStockInfo.column_stock,num)
			    			.set(ProductStockInfo.column_gxsj, new Date());
			    		if(!psi.update())
			    		{
			    			log.debug("更新库存明细失败");
							throw new RuntimeException("更新库存明细失败!");
			    		}
			    		
			    	}else
			    	{
			    		ProductStockInfo psInfo=new ProductStockInfo();
			    		String psinfoids = ToolRandoms.getUuid(true);
			    		psInfo.set(ProductStockInfo.column_ids, psinfoids)
			    				.set(ProductStockInfo.column_organids, organIds)
			    				.set(ProductStockInfo.column_productID, "c3df895ee3b24d1193cb8156edc665b7")
			    				.set(ProductStockInfo.column_type,po.getStr(ProductOutin.column_typeIds))
			    				.set(ProductStockInfo.column_stock, ps.getFloat(ProductStock.column_num))
			    				.set(ProductStockInfo.column_gxsj, new Date());
			    		if(!psInfo.save())
				    	{
			    			log.debug("保存产品库存记录失败！！");
				    		throw new RuntimeException("保存产品库存记录失败!");
				    	}	
			    	}
				
			
				}
				if(serviceShaping!=null){
					dnt.set(DailyNursingTotal.column_sycprs, dnt.getInt(DailyNursingTotal.column_sycprs)-1);
					if(isXgk){
						dnt.set(DailyNursingTotal.column_xgkjhrs, dnt.getInt(DailyNursingTotal.column_xgkjhrs)-1);
					}
					dnt.update();
			
					/**
					 * 将按摩膏和精油加回顾客产品剩余
					 */
					CustomerProduct customerProductjy = CustomerProduct.dao.findFirst(sqlProductjy,serviceShaping.getStr(ServiceShaping.column_customerIds),organIds);
					CustomerProduct customerProductg = CustomerProduct.dao.findFirst(sqlProductg,serviceShaping.getStr(ServiceShaping.column_customerIds),organIds);
					if(serviceShaping.getFloat(ServiceShaping.column_ybjyusage)!=null){
						 ybjy = serviceShaping.getFloat(ServiceShaping.column_ybjyusage);
						 BigDecimal bd = new BigDecimal(sumjy+ybjy); 
						 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
						// sumjy = sumjy +ybjy;
					}
					if(serviceShaping.getFloat(ServiceShaping.column_gbjyusage)!=null){
						 gbjy = serviceShaping.getFloat(ServiceShaping.column_gbjyusage);
						 BigDecimal bd = new BigDecimal(sumjy+gbjy); 
						 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
						// sumjy = sumjy +gbjy;
					}
					if(serviceShaping.getFloat(ServiceShaping.column_dtjyusage)!=null){
						 dtjy = serviceShaping.getFloat(ServiceShaping.column_dtjyusage);
						 BigDecimal bd = new BigDecimal(sumjy+dtjy); 
						 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
						// sumjy = sumjy +dtjy;
					}
					if( serviceShaping.getFloat(ServiceShaping.column_xtjyusage)!=null){
						 xtjy = serviceShaping.getFloat(ServiceShaping.column_xtjyusage);
						 BigDecimal bd = new BigDecimal(sumjy+xtjy); 
						 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
						// sumjy = sumjy +xtjy;
					}
					
					if(bb!=null){
						if(serviceShaping.getFloat(ServiceShaping.column_bbjyusage)!=null){
							 bbjy = serviceShaping.getFloat(ServiceShaping.column_bbjyusage);
							 BigDecimal bd = new BigDecimal(sumjy+bbjy); 
							 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
							// sumjy = sumjy +bbjy;
						}
					}
					if(kb!=null){
						if(serviceShaping.getFloat(ServiceShaping.column_kbjyusage)!=null){
							 kbjy = serviceShaping.getFloat(ServiceShaping.column_kbjyusage);
							 BigDecimal bd = new BigDecimal(sumjy+kbjy); 
							 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
							// sumjy = sumjy +kbjy;
						}
					}
					if(fr!=null){
						if(serviceShaping.getFloat(ServiceShaping.column_zfrjyusage)!=null){
							 zfrjy = serviceShaping.getFloat(ServiceShaping.column_zfrjyusage);
							 BigDecimal bd = new BigDecimal(sumjy+zfrjy); 
							 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
							// sumjy = sumjy +zfrjy;
						}
						if( serviceShaping.getFloat(ServiceShaping.column_yfrjyusage)!=null){
							 yfrjy = serviceShaping.getFloat(ServiceShaping.column_yfrjyusage);
							 BigDecimal bd = new BigDecimal(sumjy+yfrjy); 
							 sumjy= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();   
							// sumjy = sumjy +yfrjy;
						}
					}
					
					
					
					
					if(serviceShaping.getFloat(ServiceShaping.column_ybgusage)!=null){
						 ybg = serviceShaping.getFloat(ServiceShaping.column_ybgusage);
						 BigDecimal bd = new BigDecimal(sumg+ybg); 
						 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
						 //sumg = sumg+ybg;
					}
					if(serviceShaping.getFloat(ServiceShaping.column_gbgusage)!=null){
						 gbg = serviceShaping.getFloat(ServiceShaping.column_gbgusage);
						 BigDecimal bd = new BigDecimal(sumg+gbg); 
						 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
						// sumg = sumg+gbg;
					}
					if(serviceShaping.getFloat(ServiceShaping.column_dtgusage)!=null){
						 dtg = serviceShaping.getFloat(ServiceShaping.column_dtgusage);
						 BigDecimal bd = new BigDecimal(sumg+dtg); 
						 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
						// sumg = sumg+tdg;
					}
					if(serviceShaping.getFloat(ServiceShaping.column_xtgusage)!=null){
						xtg = serviceShaping.getFloat(ServiceShaping.column_xtgusage);
						 BigDecimal bd = new BigDecimal(sumg+xtg); 
						 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
						// sumg = sumg+xyg;
					}
					
					if(bb!=null){
					if(serviceShaping.getFloat(ServiceShaping.column_bbgusage)!=null){
						 bbg = serviceShaping.getFloat(ServiceShaping.column_bbgusage);
						 BigDecimal bd = new BigDecimal(sumg+bbg); 
						 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
						// sumg = sumg+bbg;
					}
					}
					if(kb!=null){
						if(serviceShaping.getFloat(ServiceShaping.column_kbgusage)!=null){
							 kbg = serviceShaping.getFloat(ServiceShaping.column_kbgusage);
							 BigDecimal bd = new BigDecimal(sumg+kbg); 
							 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
							 //sumg = sumg+kbg;
						}
					}
					if(fr!=null){
						if(serviceShaping.getFloat(ServiceShaping.column_zfrgusage)!=null){
							 zfrg = serviceShaping.getFloat(ServiceShaping.column_zfrgusage);
							 BigDecimal bd = new BigDecimal(sumg+zfrg); 
							 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
							// sumg = sumg+zfrg;
						}
						if( serviceShaping.getFloat(ServiceShaping.column_yfrgusage)!=null){
							 yfrg = serviceShaping.getFloat(ServiceShaping.column_yfrgusage);
							 BigDecimal bd = new BigDecimal(sumg+yfrg); 
							 sumg= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue(); 
							 //sumg = sumg+yfrg;
						}
						
					}
					
					
					if(sumg!=0){
						float oldExp = customerProductg.getFloat(CustomerProduct.column_totalExperience);
						BigDecimal bd = new BigDecimal(sumg+oldExp); 
						oldExp= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();  
						customerProductg.set(CustomerProduct.column_totalExperience, oldExp)
						.set(CustomerProduct.column_updateId, userIds)
						.set(CustomerProduct.column_updateDate, new Date());
						flag=customerProductg.update();
					}
					
					if(sumjy!=0){
						float oldExp = customerProductjy.getFloat(CustomerProduct.column_totalExperience);
						BigDecimal bd = new BigDecimal(sumjy+oldExp); 
						oldExp= bd.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();  
						customerProductjy.set(CustomerProduct.column_totalExperience, oldExp)
						.set(CustomerProduct.column_updateId, userIds)
						.set(CustomerProduct.column_updateDate, new Date());
						flag=customerProductjy.update();
					}
				}
			}
				customerService.set(CustomerService.column_dataStatus, 0)
				.set(CustomerService.column_updateDate, new Date())
				.set(CustomerService.column_updateId, userIds);
				flag = customerService.update();
				if(serviceShaping!=null){
					serviceShaping.set(ServiceShaping.column_dataStatus, 0)
					.set(ServiceShaping.column_updateDate, new Date())
					.set(ServiceShaping.column_updateId, userIds);
					flag = serviceShaping.update();
				}
				
				if(flag = true){
					if(cdet.getStr(CustomerDetail.column_customerType)!=null&&!cdet.getStr(CustomerDetail.column_customerType).equals("1")){
						
					
					String sqllast = getSqlMy(CustomerService.sqlId_selLastWeight);
					float weight = 0f;
					CustomerService last = CustomerService.dao.findFirst(sqllast ,customerService.getDate(CustomerService.column_startTime),customerIds);
					if(last!=null){
						weight = last.getFloat(CustomerService.column_weight);
					}else{
						weight = cdet.getFloat(CustomerDetail.column_firstWeight);
					}
					
					/*if (cs0 != null) {
						cs0.set(CustomerService.column_lastWeight, weight);
						cs0.set(CustomerService.column_updateDate, new Date());
						cs0.set(CustomerService.column_updateId,userIds);
						flag =cs0.update();
					}*/
					
					
					
					servicesql = getSqlMy(CustomerService.sqlId_selMinByCus);
					CustomerService cs1 = CustomerService.dao.findFirst(servicesql, customerIds);
					if (cs1 != null) {
						cdet.set(CustomerDetail.column_minDate, cs1.getDate(CustomerService.column_startTime));
						cdet.set(CustomerDetail.column_minWeight, cs1.getFloat(CustomerService.column_weight));
						cdet.set(CustomerDetail.column_updateDate, new Date());
						cdet.set(CustomerDetail.column_updateId, userIds);
						flag =cdet.update();
					}else{
						cdet.set(CustomerDetail.column_minDate, cdet.getDate(CustomerDetail.column_firstDate));
						cdet.set(CustomerDetail.column_minWeight, cdet.getFloat(CustomerDetail.column_firstWeight));
						cdet.set(CustomerDetail.column_updateDate, new Date());
						cdet.set(CustomerDetail.column_updateId, userIds);
						flag =cdet.update();
					}
					
					
					servicesql = getSqlMy(CustomerService.sqlId_selNowByCus);
					CustomerService cs2 = CustomerService.dao.findFirst(servicesql, customerIds);
					if (cs2 != null) {
						cdet.set(CustomerDetail.column_toStoreDate, cs2.getDate(CustomerService.column_startTime));
						cdet.set(CustomerDetail.column_nowWeight, cs2.getFloat(CustomerService.column_weight));
						cdet.set(CustomerDetail.column_updateDate, new Date());
						cdet.set(CustomerDetail.column_updateId, userIds);
						flag = cdet.update();
					}else{
						cdet.set(CustomerDetail.column_toStoreDate, cdet.getDate(CustomerDetail.column_firstDate));
						cdet.set(CustomerDetail.column_nowWeight, cdet.getFloat(CustomerDetail.column_firstWeight));
						cdet.set(CustomerDetail.column_updateDate, new Date());
						cdet.set(CustomerDetail.column_updateId, userIds);
						flag =cdet.update();
					}
					Date date =  customerService.getDate(CustomerService.column_startTime);
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
					String gxsj=sdf.format(date); 
					CustomerServiceNurse csmn = CustomerServiceNurse.dao.findFirst(" SELECT * FROM sb_customer_service_nurse WHERE customerID = ? and  YearMon = ?   LIMIT 1", customerIds,gxsj.substring(0, 7));
					BinShopColServiceService binShopColServiceService = new BinShopColServiceService();
					binShopColServiceService.saveOrUpdateNurse(csmn, customerService, cdet,1,customerIds,0,isdndyXgk);
					CustomerServiceMonthNurse monthNurse = CustomerServiceMonthNurse.dao.findFirst("select * from sb_customer_service_month_nurse where customerIds = ? and organIds = ? and `year` = ? and dataStatus in ('1','2')",customerIds,organIds,gxsj.substring(0,4));
					binShopServiceService.saveOrUpdateMonthNurse(monthNurse,customerService,cdet,1,userIds,0);
				}
		}
				
				
				}
		
		
		
		}
			
			
		
		
		
		
		
		return flag;

		
		/*if(ybgusage!=null && !ybgusage.equals("")){
			 ybg = Float.parseFloat(ybgusage);
		}
		if(bbgusage!=null && !bbgusage.equals("")){
			bbg = Float.parseFloat(bbgusage);	
		}
		if(kbgusage!=null && !kbgusage.equals("")){
			kbg = Float.parseFloat(kbgusage);
		}
		if(gbgusage!=null && !gbgusage.equals("")){
			gbg = Float.parseFloat(gbgusage);
		}
		if(zfrgusage!=null && !zfrgusage.equals("")){
			zfrg = Float.parseFloat(zfrgusage);	
		}
		if(yfrgusage!=null && !yfrgusage.equals("")){
			yfrg = Float.parseFloat(yfrgusage);
		}
		if(dtgusage!=null && !dtgusage.equals("")){
			dtg = Float.parseFloat(dtgusage);
		}
		if(xtgusage!=null && !xtgusage.equals("")){
			xtg = Float.parseFloat(xtgusage);
		}
		
		
		if(ybjyusage!=null && !ybjyusage.equals("")){
			ybg = Float.parseFloat(ybjyusage);
		}
		if(bbjyusage!=null && !bbjyusage.equals("")){
			bbg = Float.parseFloat(bbjyusage);
		}
		if(kbjyusage!=null && !kbjyusage.equals("")){
			kbg = Float.parseFloat(kbjyusage);
		}
		if(gbjyusage!=null && !gbjyusage.equals("")){
			gbg = Float.parseFloat(gbjyusage);
		}
		if(zfrjyusage!=null && !zfrjyusage.equals("")){
			zfrg = Float.parseFloat(zfrjyusage);
		}
		if(yfrjyusage!=null && !yfrjyusage.equals("")){
			yfrg = Float.parseFloat(yfrjyusage);
		}
		if(dtjyusage!=null && !dtjyusage.equals("")){
			dtg = Float.parseFloat(dtjyusage);
		}
		if(xtjyusage!=null && !xtjyusage.equals("")){
			xtg = Float.parseFloat(xtjyusage);
		}*/
		
	
	}
}
